package user

import (
	"github.com/gogf/gf/encoding/gjson"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"github.com/gogf/gfer/app/service/question"
	"github.com/gogf/gfer/app/service/user"
)

// 控制器
type C struct{}

// 展示个人资料
func (c *C) Index(r *ghttp.Request) {
	uid := r.GetInt("uid")
	if uid <= 0 {
		panic("invalid uid")
	}
	r.Response.WriteTplDefault(g.Map{
		"profile": user.GetProfile(uid),
	})
}

// 保存个人资料
func (c *C) SaveProfile(r *ghttp.Request) {

}

// 我的问题列表
func (c *C) Question(r *ghttp.Request) {
	uid := r.GetInt("uid")
	if uid <= 0 {
		panic("invalid uid")
	}
	r.Response.WriteTplDefault(g.Map{
		"questions": question.GetListByUid(uid, r.GetInt("page")),
	})
}

// 我的回答列表
func (c *C) Answer(r *ghttp.Request) {
	uid := r.GetInt("uid")
	if uid <= 0 {
		panic("invalid uid")
	}
	r.Response.WriteTplDefault(g.Map{})
}

// 返回 cookie 中的 user 信息
func (c *C) Me(r *ghttp.Request) {
	userInfo := r.Session.Get("userInfo", "{}")
	userJson, _ := gjson.LoadJson(userInfo)
	r.Response.WriteJson(g.Map{"code": 0, "data": userJson})
}
